// 登录验证
function checkLogin(i) {
	var msg = layer.msg('正在检测登录状态...');
	$.ajax({
		url: '/pan/checkLogin',
		type: 'get',
		dataType: 'json',
		async: false,
		timeout: 10000,
		success: function(data){
			if(data.statusCode == 401){
				var index = layer.alert('你还没有登录，请先登录', {closeBtn: 0}, function(){
					window.location.href = '/';
					layer.close(index);
				});
			}else{
				layer.close(msg);
			}
		},
		error: function(){
			i = typeof('undefined') ? 0 : i;
			if(i == 3){
				window.location.href = '/';
				return;
			}

			layer.msg('检测登录状态失败，正在重试...');
			checkLogin(++i);
		}
	});
}

// 格式化日期
Date.prototype.format = function(){
	var add0 = function(num){
		if(num < 10) return "0" + num;
		return "" + num;
	};

	var dateStr = "";
	dateStr += this.getFullYear() + "-";
	dateStr += add0(this.getMonth() + 1) + "-";
	dateStr += add0(this.getDate()) + " ";

	dateStr += add0(this.getHours()) + ":";
	dateStr += add0(this.getMinutes()) + ":";
	dateStr += add0(this.getSeconds());

	return dateStr;
};

// 转义HTML字符
function encodeHTML(str){
	str = "" + str;

	var t = str.split("&");

	t = t.map(function(e){
		return e.replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;").replace(" ", "&nbsp;");
	});
	
	str = t.join("&amp;");
	return str;
}
String.prototype.encodeHTML = function(){
	return encodeHTML(this);
};

// 取文件名的文件图标
function getFileIcon(filename){
	if(typeof(filename) == "undefined"){
		return "";
	}

	filename += "";

	var testReg = function(ext){
		var ext = ext.split("|");

		for (var i = 0; i < ext.length; i++) {
			if(filename.toLowerCase().endsWith("." + ext[i].toLowerCase())){
				return true;
			}
		}

		return false;
	};

	var fileicon = "";
	if(testReg("jpg|png|gif|bmp|jpeg|jpe|ico")){
		fileicon = "image";
	}else if(testReg("mp3|ape|wav|arm|mid|ogg")){
		fileicon = "music";
	}else if(testReg("mp4|avi|rmvb|3gp|flv|wmv|mpg")){
		fileicon = "video";
	}else if(testReg("doc|dot|docx|wps")){
		fileicon = "word";
	}else if(testReg("xls|xlsx")){
		fileicon = "excel";
	}else if(testReg("ppt|pptx")){
		fileicon = "ppt";
	}else if(testReg("txt|log|text")){
		fileicon = "txt";
	}else if(testReg("exe|bat|cmd|apk|ipa|mrp|sis|sisx")){
		fileicon = "app";
	}else if(testReg("zip|rar|gzip|gz|jar|tar|war")){
		fileicon = "zip";
	}

	return fileicon;
}

//转换文件大小为易读的文本
function smartSize(filesize){
	var text = "";

	if(filesize >= 1073741824){
		text = (filesize / 1073741824).toFixed(2) + "G";
	}else if(filesize >= 1048576){
		text = (filesize / 1048576).toFixed(2) + "M";
	}else if(filesize >= 1024){
		text = (filesize / 1024).toFixed(2) + "K";
	}else{
		if(typeof(filesize) == 'undefined'){
			text = '-';
		}else{
			text = filesize + "B";
		}
	}

	return text;
}

//判断变量是否定义
function defined(variable){
	return typeof(variable) != "undefined";
}

// 解析hash
function translateHash(){
	var hash = window.location.hash;

	if(!hash || !hash.replace(/^#+/, "")) return {};

	hash = hash.substring(1);
	var result = {action: "", params: {}};
	var split = hash.split("?", 2);
	result.action = split[0];
	
	if(!split[1]) return result;

	var split2 = split[1].split("&");

	var params = {};
	for (var i = 0; i < split2.length; i++) {
		if(!split2[i]) continue;
		var s = split2[i].split("=", 2);

		if(!s[0]) continue;
		params[s[0]] = defined(s[1]) ? s[1] : "";
	}
	
	result.params = params;
	return result;
}

//设置Cookie的方法
function setCookie(c_name,value){
	var expiredays = 1;
	var exdate=new Date()
	exdate.setDate(exdate.getDate()+expiredays)
	document.cookie=c_name+ "=" +escape(value)+
		((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

//获取Cookie的方法
function getCookie(c_name){
	if (document.cookie.length > 0){
		c_start = document.cookie.indexOf(c_name + "=");
		if (c_start != -1){
			c_start = c_start + c_name.length + 1;
			c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}

//将13位的时间戳转换为时间格式
function getTime(timestamp) {
	var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	var Y = date.getFullYear() + '-';
	var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
	var D = date.getDate() + ' ';
	var h = date.getHours() + ':';
	var m = date.getMinutes() + ':';
	var s = date.getSeconds();
	return Y+M+D+h+m+s;
}